package cn.daniel.framework.gateway.filter;
/*

import cn.daniel.framework.core.constants.CommonConstants;
import cn.daniel.framework.core.enums.ApiResultCode;
import cn.hutool.crypto.SmUtil;
import cn.hutool.setting.dialect.Props;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

*/
/**
 * @author daniel.tuen
 * @date 2025/9/1 15:38
 * @description 请求头签名校验
 *//*

public class SignInterceptor implements HandlerInterceptor {


    */
/**
     * app secret 配置文件名
     *//*

    private static final String FILE_NAME = "appsecret.properties";

    */
/**
     * 时间戳有效时间
     *//*

    @Value("${ctj.encrypt.validTime:180000}")
    private Long validTime;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        // 校验请求头参数: appId
        String appId = request.getHeader(CommonConstants.APP_ID);
        if(StringUtils.isBlank(appId)){
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, ApiResultCode.PARAM_INVALID_APP_ID.getMessage());
            return false;
        }

        // 校验请求头参数: timestamp
        String timestamp = request.getHeader(CommonConstants.TIMESTAMP);
        if(StringUtils.isBlank(timestamp)){
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, ApiResultCode.PARAM_INVALID_TIMESTAMP.getMessage());
            return false;
        }

        // 校验请求头参数: sign
        String sign = request.getHeader(CommonConstants.SIGN);
        if(StringUtils.isBlank(sign)){
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, ApiResultCode.PARAM_INVALID_SIGN.getMessage());
            return false;
        }

        // 校验请求头参数: version
        String version = request.getHeader(CommonConstants.VERSION);
        if(StringUtils.isBlank(version)){
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, ApiResultCode.PARAM_INVALID_VERSION.getMessage());
            return false;
        }

        // 校验时间戳是否间隔大于三分钟
        long currentTimeMillis = System.currentTimeMillis();
        long timestampParam = Long.parseLong(timestamp);
        long tempTime = Math.abs(currentTimeMillis - timestampParam);
        if(tempTime > validTime){
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, ApiResultCode.PARAM_INVALID_TIMESTAMP.getMessage());
            return false;
        }

        // 校验签名是否正确
        Props props = new Props(FILE_NAME);
        String appSecret = props.getProperty(appId);
        StringBuffer buffer = new StringBuffer();
        buffer.append(appId).append(appSecret).append(timestamp).append(version);
        String content = buffer.toString();
        String verify = SmUtil.sm3(content).toUpperCase();
        if(!verify.equals(sign)){
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, ApiResultCode.APP_SIGN_FAIL.getMessage());
            return false;
        }
        return Boolean.TRUE;
    }
}
*/
